//
//  AdvertPage.m
//  HB
//
//  Created by 孙智慧 on 16/1/26.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "AdvertPage.h"
#import "AppDelegate.h"
#import "DBHelper.h"
#import "AdvertOperation.h"

@implementation AdvertPage

-(void) opSuccessForDownLoad:(id)data andFlag:(NSString *)flag
{

}

-(void) opFail:(NSString *)errorMessage
{

}

-(void) opSuccessForPost:(id)data
{

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //[self getAdvertImage];
    
    [self checkUserIsIn];
}

-(void)getAdvertImage
{
    operation=[[AdvertOperation alloc]initWithDelegate:self];
    
    NSString *url= [FxGlobal getServiceUrl:@"advert" andFuncName:@"AdvertList"];
    
    NSDictionary *dictInfo = @{@"url":url};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:0];
    
    [Common createActivityView: self.imgAdvert];
}

-(void) opSuccessForGet:(id)data andFlag:(NSInteger)flag
{
    
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *dataParse  = [dataStr objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
    
    if ([[dataParse objectForKey:@"result"] isEqualToString:@"1"])
    {
        NSArray *dataArray= [dataParse objectForKey:@"data"];
        
        for (NSInteger i=0; i<[dataArray count]; i++)
        {
            
            NSDictionary *dict= [dataArray objectAtIndex:i];
            
            NSString *imgName= [dict objectForKey:@"Title"];
            
            NSString *imgPath= [dict objectForKey:@"URL"];
            
            [Common downLoadImageByURL:self.imgAdvert andURL:imgPath andName:imgName];
            
        }
    }
}

- (void)delayHideAdvert
{
    [self performSelector:@selector(hideLanch) withObject:nil afterDelay:3];
}

- (void)hideLanch
{
    if (self.view.superview != [AppDelegate appDeg].window)
    {
        [self.view removeFromSuperview];
    }
    else
    {
        [self checkUserIsIn];
    }
}


-(void) checkUserIsIn
{
    DBHelper *db=[DBHelper sharedMyClass];
    
    NSString *dbPath = [FxGlobal getUserDBFile];
    
    [db createDB:dbPath];
    
    [db createTable];
    
    NSMutableDictionary *res= [db queryData];
    
    if([res count]>0)
    {
        if([[res objectForKey:@"login" ] boolValue] == YES)
        {
            [[AppDelegate appDeg] showHomePage:YES];
        }
        else
        {
            [[AppDelegate appDeg] showHomePage:NO];
        }
    }
    else
    {
        [[AppDelegate appDeg] showHomePage:NO];
    }
    
}

+ (void)showAdvertPage
{
    AdvertPage *controller = [[AdvertPage alloc] init];
    
    UIWindow *window = [AppDelegate appDeg].window;
    
    if (window.rootViewController != nil)
    {
        CGRect frame = window.rootViewController.view.bounds;
        
        controller.view.frame = frame;
        
        [window.rootViewController.view addSubview:controller.view];
    }
    else
    {
        window.rootViewController = controller;
        
        [window makeKeyAndVisible];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}





@end
